package cn.edu.tsinghua.training.cms.demo.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import cn.edu.tsinghua.training.cms.demo.dao.GenericDao;
import cn.edu.tsinghua.training.cms.demo.entity.Admin;
import cn.edu.tsinghua.training.cms.demo.service.AdminService;

@Service
public class AdminServiceImpl extends GenericServiceImpl<Admin, Integer> implements AdminService {

	@Override
	@Autowired
	@Qualifier("adminDaoImpl")
	public void setGenericDao(GenericDao<Admin, Integer> genericDao) {
		super.setGenericDao(genericDao);
	}
	
	@Override
	public Admin login(Admin admin) {
		String hql = "from Admin where username=:username and password=:password";
		String[] paramNames = {"username", "password"};
		Object[] values = {admin.getUsername(), DigestUtils.md5DigestAsHex(admin.getPassword().getBytes())};
		List<Admin> admins = genericDao.query(hql, paramNames, values);
		if (admins.size() == 1) {
			return admins.get(0);
		} else {
			return null;
		}
	}

}
